package vip.liux.backend.adapter.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import vip.liux.backend.infrastructure.utils.JwtConverter;
import vip.liux.contracts.security.UserDetailsInfo;

/**
 * 账户通行证控制器
 */
@RestController
@RequestMapping("/api/account")
public class AccountController {

    private static final Logger log = LoggerFactory.getLogger(AccountController.class);

    @GetMapping("/passport/logout")
    public void logout() {
        log.info("logout");
    }

    // region Info

    /**
     * 获取当前用户
     *
     * @param authentication 认证信息
     * @return 当前用户
     * @see org.springframework.security.core.userdetails.User
     * @see org.springframework.security.oauth2.jwt.Jwt
     */
    @GetMapping("/authentication")
    public String register(Authentication authentication) {
        log.info("current user: {}", authentication.getName());
        return authentication.getName();
    }

    /**
     * 获取token
     *
     * @param authentication 认证信息
     * @return token
     */
    @PostMapping("/authentication/token")
    public JwtConverter.TokenModel authenticationToken(Authentication authentication) {
        UserDetailsInfo userDetail = (UserDetailsInfo) authentication.getPrincipal();
        return JwtConverter.generateToken(userDetail);
    }
    // endregion
}